﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.Mvc;
using Dragon.Prototype1.Infrastructure.Controls;

using Dragon.Prototype1.Service;
using LINQtoSQLDataProvider;

namespace Dragon.Prototype1.Infrastructure.FormBuilder
{
    public class FormGroup : jQueryControl
    {
        public FormGroup(string id)
        {
            this.TableCssClass = "tabForm ui-widget-content";
            this.ColumnNumber = 2;
            this.ID = id;
        }

        public string Width
        {
            get;
            set;
        }
        public int ColumnNumber
        {
            get;
            set;
        }
        public string TableCssClass
        {
            get;
            set;
        }
        protected override System.Web.UI.HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        List<FormRow> formRowCollection = new List<FormRow>();
        public List<FormRow> Rows
        {
            get { return formRowCollection; }
        }

        bool prevOccupyWholeRow = false;
        private void InsertRowToForm(bool occupyWholeRow)
        {
            if (tempRow!=null&&(columnCount == ColumnNumber || occupyWholeRow||prevOccupyWholeRow))
            {
                //add the previous row to the Rows collection
                this.Rows.Add(tempRow);
                //create a new row
                tempRow = null;
                columnCount = 0;
            }
            if (tempRow == null)
            {
                tempRow = new FormRow(this.ColumnNumber);
            }
            prevOccupyWholeRow = occupyWholeRow;
        }

        int columnCount = 0;
        FormRow tempRow = null;
        #region PopupBox
        public void AddLabeledPopupBox(string id, string labeltext, PopupBox pbbox,bool isrequired, bool occupyWholeRow)
        {
            InsertRowToForm(occupyWholeRow);
            Label newLabel = new Label(id, labeltext);
            tempRow.AddControl(newLabel, pbbox, isrequired).OccupyWholeRow = occupyWholeRow;

            columnCount++;
        }
        #endregion

        #region Literal
        public void AddLabeledLiteral(string labeltext,string valuetext, bool occupyWholeRow)
        {
            InsertRowToForm(occupyWholeRow);
            Label newLabel = new Label(labeltext);
            Literal ltl = new Literal(valuetext);

            tempRow.AddControl(newLabel,ltl).OccupyWholeRow = occupyWholeRow;

            columnCount++;
        }
        public void AddLabeledOptionText(string typeCode, string selectedValue, bool occupyWholeRow)
        {
            DataDictionary parentEntry = DataDictionaryService.GetParentEntry(typeCode);
            if (parentEntry == null)
            {
                throw new InvalidOperationException("Entry not found,please double-check your type code");
            }
            DataDictionary dd = parentEntry.DataDictionaries.Single(d => d.Value == selectedValue);
            this.AddLabeledLiteral(DataDictionaryResource(parentEntry.Text),DataDictionaryResource(dd.Text),occupyWholeRow);

            DataDictionaryService.DisposeDataContext();
        }
        public void AddLabeledBooleanText(string labeltext,bool bValue, bool occupyWholeRow)
        {
            string valueHTML;
            if(bValue)
            {
               valueHTML="<img src=\"/Images/chk_on.png\" />";
            }else
            {
               valueHTML="<img src=\"/Images/chk_off.png\" />";
            }
            this.AddLabeledLiteral(labeltext,valueHTML , occupyWholeRow);

            DataDictionaryService.DisposeDataContext();
        }

        #endregion
        #region TextBox
        public void AddLabeledTextBox(string id, string labeltext, string valuetext, string ctrlCssClass, bool isrequired, bool occupyWholeRow,bool isMultiLine, bool isPasswordBox)
        {
            InsertRowToForm(occupyWholeRow);
            TextBox newTb = new TextBox(id, valuetext,isMultiLine,isPasswordBox);
            newTb.Rows = 5;
            newTb.Columns = 50;
            newTb.CssClass = ctrlCssClass;
            Label newLabel = new Label(id, labeltext);
            tempRow.AddControl(newLabel, newTb, isrequired).OccupyWholeRow = occupyWholeRow;

            columnCount++;
        }

        public void AddLabeledTextBox(string id, string labeltext, string valuetext,bool isrequired)
        {
            this.AddLabeledTextBox(id, labeltext, valuetext, "normalinput", isrequired, false, false, false);
        }
        public void AddLabeledTextBox(string id, string labeltext, bool isrequired)
        {
            this.AddLabeledTextBox(id, labeltext, string.Empty, "normalinput", isrequired, false,false,false);
        }
        public void AddLabeledTextBox(string id, string labeltext, bool isrequired, bool occupyWholeRow)
        {
            this.AddLabeledTextBox(id, labeltext, string.Empty, "normalinput", isrequired, occupyWholeRow,false,false);
        }

        public void AddLabeledTextBox(string id, string labeltext, string valuetext, bool isrequired, bool occupyWholeRow)
        {
            this.AddLabeledTextBox(id, labeltext, valuetext, "normalinput", isrequired, occupyWholeRow, false, false);
        }

        public void AddLabeledPasswordBox(string id, string labeltext, bool isrequired)
        {
            this.AddLabeledTextBox(id, labeltext, string.Empty, "normalinput", isrequired, false, false, true);
        }

        public void AddLabeledPasswordBox(string id, string labeltext, bool isrequired, bool occupyWholeRow)
        {
            this.AddLabeledTextBox(id, labeltext, string.Empty, "normalinput", isrequired, occupyWholeRow, false, true);
        }
        #endregion

        #region DatePicker
        public void AddLabeledDatePicker(string id, string labeltext, string datevalue, string ctrlCssClass, bool isrequired, bool useButton, bool occupyWholeRow)
        {
            InsertRowToForm(occupyWholeRow);

            Label newLabel = new Label(id, labeltext);
            DatePickerBox datepicker = new DatePickerBox(id);
            if (useButton)
            {
                datepicker.Selector = "#" + id;
                datepicker.ShowOn = DatePickerShowOn.both;
                datepicker.ButtonImagePath = "/Images/date.png";
                datepicker.ButtonImageOnly = true;
            }
            datepicker.CssClass = ctrlCssClass;
            tempRow.AddControl(newLabel, datepicker, isrequired).OccupyWholeRow = occupyWholeRow;

            columnCount++;
        }
        public void AddLabeledDatePicker(string id, string labeltext, bool isrequired)
        {
            this.AddLabeledDatePicker(id, labeltext, string.Empty, "normalinput", isrequired, true, false);
        }
        public void AddLabeledDatePicker(string id, string labeltext, bool isrequired, bool occupyWholeRow)
        {
            this.AddLabeledDatePicker(id, labeltext, string.Empty, "normalinput", isrequired, true, occupyWholeRow);
        }
        #endregion

        #region Checkbox
        public void AddLabeledCheckbox(string name, string labeltext, bool isrequired)
        {
            this.AddLabeledCheckbox(name, labeltext, false, isrequired, false);
        }

        public void AddLabeledCheckbox(string name, string labeltext, bool isChecked, bool isrequired)
        {
            this.AddLabeledCheckbox(name, labeltext, isChecked, isrequired, false);
        }
        public void AddLabeledCheckbox(string name, string labeltext,bool isChecked, bool isrequired, bool occupyWholeRow)
        {
            InsertRowToForm(occupyWholeRow);

            Label newLabel = new Label(name, labeltext);
            CheckBox checkbox = new CheckBox(name);

            tempRow.AddControl(newLabel, checkbox, isrequired).OccupyWholeRow = occupyWholeRow;

            columnCount++;
        }
        public void AddLabeledCheckboxList(string name, string labeltext,List<string> checkboxLabels,List<string> checkboxValues,List<bool> ischeckedlist ,bool isrequired, bool occupyWholeRow)
        {
            InsertRowToForm(occupyWholeRow);

            Label newLabel = new Label(name, labeltext);
            CheckBoxList checkboxlist = new CheckBoxList(name,string.Empty);
            checkboxlist.DataBind(checkboxLabels, checkboxValues,ischeckedlist);

            tempRow.AddControl(newLabel, checkboxlist, isrequired).OccupyWholeRow = occupyWholeRow;

            columnCount++;
        }
        #endregion

        internal static string DataDictionaryResource(string resourceKey)
        {
            return ResourceHtmlHelpers.Resource("DataDictionary", resourceKey);
        }

        #region DropDownList

        public void AddLabeledDropDownList(string id, string typeCode, bool isrequired, bool occupyWholeRow)
        {
            this.AddLabeledDropDownList(id, typeCode, string.Empty, isrequired, occupyWholeRow);
        }

        public void AddLabeledDropDownList(string id, string typeCode,string selectedValue,bool isrequired,bool occupyWholeRow)
        {
            DataDictionary parentEntry=DataDictionaryService.GetParentEntry(typeCode);
            if(parentEntry==null)
            {
                throw new InvalidOperationException("Entry not found,please double-check your type code");
            }
            List<SelectListItem> list=new List<SelectListItem>();
            //add default select item
            SelectListItem defaultitem = new SelectListItem();
            defaultitem.Text = DataDictionaryResource("DEFAULTSELECT");
            list.Add(defaultitem);
            //add sub items
            foreach(DataDictionary entry in parentEntry.DataDictionaries.ToList<DataDictionary>())
            {
                SelectListItem item=new SelectListItem();
                item.Text=DataDictionaryResource(entry.Text);
                item.Value=entry.Value;
                if (!string.IsNullOrEmpty(selectedValue)&&entry.Value == selectedValue)
                {
                    item.Selected = true;
                }
                list.Add(item);
            }
            this.AddLabeledDropDownList(id, DataDictionaryResource(parentEntry.Text), list, isrequired, occupyWholeRow);
            DataDictionaryService.DisposeDataContext();
        }

        public void AddLabeledDropDownList(string id, string labeltext, List<SelectListItem> selectList, bool isrequired)
        {
            this.AddLabeledDropDownList(id, labeltext, selectList, false);
        }

        public void AddLabeledDropDownList(string id, string labeltext, List<SelectListItem> selectList, bool isrequired, bool occupyWholeRow)
        {
            InsertRowToForm(occupyWholeRow);

            Label newLabel = new Label(id, labeltext);
            DropDownList list = new DropDownList(id, selectList);
            tempRow.AddControl(newLabel, list, isrequired).OccupyWholeRow = occupyWholeRow;

            columnCount++;
        }
        #endregion

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(this.Width))
            {
                writer.AddStyleAttribute(HtmlTextWriterStyle.Width, this.Width);
            }
            base.AddAttributesToRender(writer);
        }

        public override void Render(HtmlTextWriter writer)
        {
            if (tempRow!=null&&tempRow.LabelAndControlCollection.Count > 0)
            {
                Rows.Add(tempRow);
                tempRow = null;
                columnCount = 0;
            }

            base.Render(writer);
        }
        public override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, TableCssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            foreach (FormRow row in formRowCollection)
            {
                row.Render(writer);
            }
            writer.RenderEndTag();
        }

        public override void RenderJavaScriptContents(HtmlTextWriter writer)
        {
            foreach (FormRow row in formRowCollection)
            {
                row.RenderJavaScriptContents(writer);
            }
        }
    }
}
